package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.model.CommentModel;
import com.app.model.JiuquanUserModel;
import com.app.service.CommentService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//共用配置
@RestController
@Controller
@RequestMapping("/jiuquan/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;


    /**
     * 查询列表
     * @param startTime 根据评论创建时间筛  开始时间
     * @param endTime   根据评论创建时间筛  结束时间
     * @param checkStatus  审核状态  0待审核 1已审核
     * @param evaluate   evaluate==commentType  评价 2好评 3中评 4差评
     * @return
     */
    @GetMapping("/getCommentList")
    public Result getCommentList(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit,String startTime,String endTime,Integer checkStatus,Integer evaluate) {
        CommentVo commentList = commentService.getCommentList(page, limit,startTime,endTime,checkStatus,evaluate);
        return Result.success(commentList);
    }
    /**
     * 根据类型获取评论列表
     * @param type  1 场地  2 导师
     * @param relationId  导师或者场地id
     * @param commentType 评论类型1.最新 2.好评 3.中评 4.差评
     * @return
     */
    @GetMapping("/getCommentByTypeList")
    public Result getCommentByTypeList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit, Integer type, Integer relationId,
                                       @RequestParam(defaultValue = "1") Integer commentType) {
        CommentVo commentList = commentService.getCommentByTypeList(page, limit, type, relationId,commentType);
        return Result.success(commentList);
    }

    //详情
    @GetMapping("/getCommentById")
    public Result getCommentById(Integer id) {
        CommentModel comment = commentService.getById(id);
        JiuquanUserModel user = userService.getUserByOpenid(comment.getOpenId());
        comment.setUserName(user.getRealName());
        comment.setFaveUrl(user.getFaceUrl());
        comment.setOpenId(null);
        Integer score = comment.getScore();
        if (score > 3) {
            comment.setScoreText("好评");//好评
        }
        if (score == 3) {
            comment.setScoreText("中评");//中评
        }
        if (score < 3) {
            comment.setScoreText("差评");//差评
        }
        return Result.success(comment);
    }

    //评论审核状态更改
    @PostMapping("/updateCheckStatus")
    public Result updateCheckStatus(@RequestBody CommentModel commentModel) {
        commentModel.setUpdateTime(CommonHelp.getNowTimestamp());
        commentService.updateById(commentModel);
        return Result.success("修改审核状态成功");
    }

    //用户新增评论
    @PostMapping("/userAddComment")
    public Result userAddComment(@RequestBody CommentModel commentModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.verify(token);
        if (commentModel.getScore() > 3) {
            commentModel.setCommentType(2);//好评
        }
        if (commentModel.getScore() == 3) {
            commentModel.setCommentType(3);//中评
        }
        if (commentModel.getScore() < 3) {
            commentModel.setCommentType(4);//差评
        }
        commentModel.setOpenId(openId);
        commentModel.setCreateTime(CommonHelp.getNowTimestamp());
        commentService.save(commentModel);
        return Result.success("添加成功");
    }
    //删除
    @GetMapping("/delComment")
    public Result delComment(Integer id) {
        commentService.removeById(id);
        return Result.success("删除成功");
    }
}
